package cn.yedong.system.service.impl;

import cn.yedong.basic.service.Impl.BaseServiceImpl;
import cn.yedong.system.domain.Menu;
import cn.yedong.system.service.IMenuService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * 业务实现类：
 */
@Service
public class MenuServiceImpl extends BaseServiceImpl<Menu> implements IMenuService {
    @Autowired
    private IMenuService menuService;
    /**
     * 获取菜单树
     * @return
     */
    @Override
    public List<Menu> getMenuTree() {
        HashMap<Long, Menu> map = new HashMap<>();
        List<Menu> menus = menuService.loadAll();
        for (Menu menu : menus) {
            map.put(menu.getId(),menu);
        }

        ArrayList<Menu> menuTree = new ArrayList<>();
        for (Menu menu : menus) {
            //是顶级部门
            if(menu.getParentId() == null){
                menuTree.add(menu);
            }else {
                Menu parentMenu = map.get(menu.getParentId());
                if(parentMenu != null){
                    parentMenu.getChildren().add(menu);
                }
            }
        }
        return menuTree;
    }
}
